package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsOrder;
import com.qf.pojo.DtsOrderGoods;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsGoodsOrderService;
import com.qf.service.DtsOrderService;
import com.qf.service.DtsUserService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin("*")
@RestController
@RequestMapping("/admin/order")
public class AdminOrderController {

    @Autowired
    DtsOrderService orderService;

    @Autowired
    DtsGoodsOrderService orderGoodsService;

    @Autowired
    DtsUserService userService;

    @RequestMapping("/detail")
    public Object detail(Integer id){
        if(id == null){
            return ResponseUtil.badArgument();
        }
        return  orderGoodsService.findById(id);

    }

    @RequestMapping("/ship")
    public Object ship(@RequestBody Map<String,Object> map){


        Integer orderId = (Integer)map.get("orderId");
        String shipChannel = (String)map.get("shipChannel");
        String shipSn = (String)map.get("shipSn");
        return orderService.addShip(orderId,shipChannel,shipSn);
    }

    @RequestMapping("/listShipChannel")
    public Object listShipChannel(){

        String [] value ={"HTKY","ZTO","YTO","YD"};
        String [] label ={"百世快递","中通快递","圆通速递","韵达速递"};
        List list = new ArrayList<>();
        for(int i =0;i < 4;i++){

            HashMap<String, Object> map = new HashMap<>();

            map.put("value",value[i]);
            map.put("label",label[i]);
            list.add(map);
        }
        HashMap<String, Object> data = new HashMap<>();
        data.put("shipChannelList",list);
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/list")
    public Object list(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                       @RequestParam(name = "limit", required = false, defaultValue = "20") Integer limit,
                       Integer orderStatusArray,
                       @RequestParam(name = "sort", required = false, defaultValue = "add_time") String sort,
                       @RequestParam(name = "order", required = false, defaultValue = "desc") String order,
                       Integer userId,
                       String orderSn){

        IPage<DtsOrder> iPage = orderService.queryPage(page, limit, orderStatusArray, sort, order, userId, orderSn);

        Map<String, Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());

        return ResponseUtil.ok(data);
    }
}
